//
//  NSObject+KMModel.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//

#import "NSObject+KMModel.h"
#import <objc/runtime.h>

@interface KMDelegateWrapper : NSObject

@property (weak,nonatomic) id delegate;

@end

@implementation KMDelegateWrapper

@end

@implementation NSObject (KMModel)

- (id)kmDelegate
{
    KMDelegateWrapper * wrapper = objc_getAssociatedObject(self, _cmd);
    return wrapper.delegate;
}

- (void)setKmDelegate:(id)kmDelegate
{
    KMDelegateWrapper * wrapper = objc_getAssociatedObject(self, @selector(kmDelegate));
    if (wrapper == nil)
    {
        wrapper = [KMDelegateWrapper new];
        wrapper.delegate = kmDelegate;
        objc_setAssociatedObject(self, @selector(kmDelegate), wrapper, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    else
    {
        wrapper.delegate = kmDelegate;
    }
}

@end
